package eu.jpereira.trainings.designpatterns.creational.builder.json;

import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBody;
import eu.jpereira.trainings.designpatterns.creational.builder.ReportBodyBuilder;

public class JSONReportBodyBuilder implements ReportBodyBuilder {

	private JSONReportBody reportBody;
	private boolean withItems = false;
	
	public JSONReportBodyBuilder() {
		
		reportBody = new JSONReportBody();
	}
	
	public ReportBodyBuilder setCustomerName(String customerName) {
		
		reportBody.addContent("sale:{customer:{");
		reportBody.addContent("name:\"");
		reportBody.addContent(customerName);
		reportBody.addContent("\"");
		return this;
	}

	public ReportBodyBuilder setCustomerPhone(String phoneNumber) {
		
		reportBody.addContent(",phone:\"");
		reportBody.addContent(phoneNumber);
		reportBody.addContent("\"}");
		return this;
	}

	public ReportBodyBuilder withItems() {
		
		if (!withItems) {
			reportBody.addContent(",items:[");
			withItems = true;
		}
		else {
			reportBody.addContent("]");
			withItems = false;
		}
		
		return this;
	}

	public ReportBodyBuilder newItem(String name, int quantity, double price) {
		
		reportBody.addContent("{name:\"");
		reportBody.addContent(name);
		reportBody.addContent("\",quantity:");
		reportBody.addContent(String.valueOf(quantity));
		reportBody.addContent(",price:");
		reportBody.addContent(String.valueOf(price));
		reportBody.addContent("}");
		reportBody.addContent(",");
		return this;
	}
	
	public ReportBodyBuilder endReport() {
		
		reportBody.addContent("}");
		return this;
	}

	public ReportBody getReportBody() {
		
		return reportBody;
	}
}
